<?php
class FileTree{
	/*
		
		== PHP FILE TREE ==
		
			Let's call it...oh, say...version 1?
		
		== AUTHOR ==
		
			Cory S.N. LaViska
			http://abeautifulsite.net/
			
		== DOCUMENTATION ==
		
			For documentation and updates, visit http://abeautifulsite.net/notebook.php?article=21
			
	*/

	public $php_file_tree = array();
	public $dir = array();
	public static function php_file_tree($directory, $return_link, $extensions = array(),$nameFolderOld = 'Demo',$uuid = null) {
		// Generates a valid XHTML list of all directories, sub-directories, and files in $directory
		// Remove trailing slash
		$code = "";
		if( substr($directory, -1) == "/" ) $directory = substr($directory, 0, strlen($directory) - 1);
		$code.= FileTree::php_file_tree_dir($directory, $return_link, $extensions,true,$nameFolderOld,$uuid);
		return $code;
	}

	public static function php_file_tree_dir($directory, $return_link, $extensions = array(), $first_call = true,$nameFolderOld,$uuid) {
		// Recursive function called by php_file_tree() to list directories/files
		
		// Get and sort directories/files
		if( function_exists("scandir") ) $file = scandir($directory); else $file = $this->php4_scandir($directory);
		natcasesort($file);
		// Make directories first
		$files = $dirs = array();
		foreach($file as $this_file) {
			if( is_dir("$directory/$this_file" ) ) $dirs[] = $this_file; else $files[] = $this_file;
		}
		$file = array_merge($dirs, $files);
		
		// Filter unwanted extensions
		if( !empty($extensions) ) {
			foreach( array_keys($file) as $key ) {
				if( !is_dir("$directory/$file[$key]") ) {
					$ext = substr($file[$key], strrpos($file[$key], ".") + 1); 
					if( !in_array($ext, $extensions) ) unset($file[$key]);
				}
			}
		}
		$php_file_tree = "";
		$dir = array();
		
		if( count($file) > 2 ) { // Use 2 instead of 0 to account for . and .. "directories"
			
			
			// $php_file_tree .= ">";
			if( !$first_call ) { 
				$php_file_tree = "<ul>";
			}
				// $php_file_tree.= '<li class="iteam li-project-name" uuid="'.$uuid.'">';
							
				// $php_file_tree.= '<input type="checkbox" id="something{{$index}}">';
				// $php_file_tree.= '<label for="something" class="ng-binding">'.$nameFolderOld.'</label>';
							
				// $php_file_tree.= '<i class="fa fa-trash-o trash-i class_remove_project{{$index}}" uuid="'.$uuid.'" ng-click="removeProject(projects,$index)"></i>';
							
				// $php_file_tree.= '</li>';
				
				// $first_call = false; 
			// }
			foreach( $file as $this_file ) {
				if( $this_file != "." && $this_file != ".." ) {
					if( is_dir("$directory/$this_file") ) {
						$rand = md5(time()).rand(0,9999999999999);
						// dump($rand);
						// Directory
						$php_file_tree .= "<li>";
						$php_file_tree .= '<input type="checkbox", id="'.$rand.'" />';
						$php_file_tree .= '<label for="'.$rand.'">'.htmlspecialchars($this_file) .'</label>';
						
						$php_file_tree .= FileTree::php_file_tree_dir("$directory/$this_file", $return_link ,$extensions, false,$nameFolderOld,$uuid);
						$php_file_tree .= "</li>";
						
					} else {
						// File
						// Get extension (prepend 'ext-' to prevent invalid classes from extensions that begin with numbers)
						$ext = "ext-" . substr($this_file, strrpos($this_file, ".") + 1); 
						$link = str_replace("[link]", "$directory/" . urlencode($this_file), $return_link);
						
						$php_file_tree .= "<li ><a href='#'>" . htmlspecialchars($this_file) . "</a></li>";
						
					}
				}
			}
			if( !$first_call ) {
				$php_file_tree .= "</ul>";
			}
		}

		return $php_file_tree;
	}

	// For PHP4 compatibility
	public static function php4_scandir($dir) {
		$dh  = opendir($dir);
		while( false !== ($filename = readdir($dh)) ) {
			$files[] = $filename;
		}
		sort($files);
		return($files);
	}
}
